package com.lwx.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

// 使用@WebServlet注解,  设定访问路径
@WebServlet("/test1")
public class DemoServlet1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
//        默认的输出流格式为 iso-8859-1, 中文会出现错误,   所以在这里设置输出流的格式
        request.setCharacterEncoding("utf-8");
        response.setContentType("utf-8");
        out.write("this is my life, I like it very much!");
        out.write("今天是一个好日子!");

//        给request的 域对象 添加响应的属性
        request.setAttribute("name","Bob");
        request.setAttribute("age", 19);

//        实现请求的转发, 跳转到 index.jsp中
        request.getRequestDispatcher("/").forward(request, response);

    }
}
